﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ThaireInvestmentWeb.Models;
using ThaireInvestmentWeb.Util;
using ThaireInvestmentWeb.WcfSerivce;

namespace ThaireInvestmentWeb.Controllers.Instrument
{
    public class EquityController : Controller
    {
        private WcfSerivce.InvestmentServiceClient _service;
        public WcfSerivce.InvestmentServiceClient Service
        {
            get { return _service ?? (_service = ServiceFactory.GetService()); }
        }

        #region ManageEquityHeader

        public ActionResult ManageEquity()
        {
            ManageEquityModel model = null;
            try
            {
                var data = Service.GetEquityHeaderScreenData(null);
                model = ManageEquityModel.BuildModel(data);
            }
            catch (Exception ex)
            {
                model = new ManageEquityModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            TempData["ManageEquityModel"] = model;
            return View("ManageEquity", model);
        }

        [HttpPost]
        public ActionResult SearchEquityHeader(ManageEquityModel model)
        {
            var oldModel = (ManageEquityModel)TempData["ManageEquityModel"];

            try
            {
                if (!String.IsNullOrEmpty(model.SearchBtn))
                {
                    oldModel.Result = Service.SearchEquity(model.SearchFilter);
                    if (oldModel.Result == null || oldModel.Result.Count == 0)
                    {
                        ViewBagUtil.SetDataNotFoundInfo(ViewBag);
                    }
                }
                else if (!String.IsNullOrEmpty(model.ResetBtn))
                {
                    ModelState.Clear();
                    oldModel.ClearFilterAndData();
                }
                TempData["ManageEquityModel"] = oldModel;
            }
            catch (Exception ex)
            {
                //ViewData["error"] = ex.Message;
                // ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            //return View("fixedIncomeLIst", oldModel);
            return View("ManageEquity", oldModel);
        }
        #endregion

        #region EquityHeader

        public ActionResult EditEquityHeader(int? headerId)
        {
            EditEquityHeaderModel model = null;
            try
            {
                var data = Service.GetEquityHeaderScreenData(headerId);
                model = EditEquityHeaderModel.BuildModel(data);
                if (data != null && data.HeaderDTO != null)
                    model.UpdateMode = true;
                TempData["EditEquityHeaderModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityHeaderModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View(model);
        }
        [HttpPost]
        public ActionResult EditEquityHeader(EditEquityHeaderModel model)
        {
            var oldModel = (EditEquityHeaderModel)TempData["EditEquityHeaderModel"];
            if (ModelState.IsValid)
            {
                try
                {
                    if (!String.IsNullOrEmpty(model.SaveBtn))
                    {
                        var company = oldModel.CompanyList.SingleOrDefault(x => x.Value == model.Header.EquityCompanyID.GetValueOrDefault().ToString());
                        if (company != null)
                            model.Header.EquityCompanyName = company.Text;

                        var EquityType = oldModel.EquityTypeList.SingleOrDefault(x => x.Value == model.Header.EquityTypeID.GetValueOrDefault().ToString());
                        if (EquityType != null)
                            model.Header.EquityTypeName = EquityType.Text;

                        var involve = oldModel.InvolvedPartyTypeNameList.SingleOrDefault(x => x.Value == model.SelectedInvolvedPartyType.ToString());
                        if (involve != null)
                            model.Header.InvolvedPartyTypeName = involve.Text;

                        if (model.UpdateMode)
                        {
                            model.Header.ModifyBY = ServiceFactory.UserId;
                            model.Header.CreateBY = oldModel.Header.CreateBY;
                            model.Header.CreateDate = oldModel.Header.CreateDate;
                            model.Header.ModifyDate = oldModel.Header.ModifyDate;
                            var result = Service.UpdateEquityHeader(model.Header);
                            oldModel.Header = result.Header;
                            oldModel.Transactions = result.Transactions;
                        }
                        else
                        {
                            model.Header.CreateBY = ServiceFactory.UserId;

                            //model.Header.RemainUnit = model.Header.

                            var result = Service.InsertEquityHeader(model.Header);
                            oldModel.Header = result.Header;
                            oldModel.Transactions = result.Transactions;
                        }
                        oldModel.UpdateMode = true;
                        var involve2 = oldModel.InvolvedPartyTypeNameList.SingleOrDefault(x => x.Text == oldModel.Header.InvolvedPartyTypeName);
                        if (involve2 != null)
                            oldModel.SelectedInvolvedPartyType = int.Parse(involve.Value);
                        ViewBagUtil.SetCompletedInfo(ViewBag);
                    }
                    else
                    {
                        throw new Exception("Button save (BtnSave) is null.");
                    }
                }
                catch (Exception ex)
                {
                    //ModelState.AddModelError("", ex.Message);
                    ViewBagUtil.SetError(ViewBag, ex.Message, null);
                }
            }
            TempData["EditEquityHeaderModel"] = oldModel;
            return View(oldModel);
        }

        public ActionResult GetInvolvedType(int id)
        {
            var oldModel = (EditEquityHeaderModel)TempData["EditEquityHeaderModel"];

            InvolvedPartyType involve = oldModel.ScreenData.InvolvedPartyTypes.Where(X => X.InvolvedPartyTypeId == id).SingleOrDefault();
            TempData["EditEquityHeaderModel"] = oldModel;
            if (involve != null)
            {
                return Json(new { InvolvedPartyTypeReference = involve.InvolvedPartyTypeReference, InvolvedPartyTypeCode = involve.InvolvedPartyTypeCode });
            }
            else
            {
                return Json(new { InvolvedPartyTypeReference = "", InvolvedPartyTypeCode = "" });
            }
        }

        public ActionResult GetFullNameEN()
        {
            var data = Service.GetEquityFullNameEN();
            return Json(data);
        }

        public ActionResult GetFullNameTH()
        {
            var data = Service.GetEquityFullNameTH();
            return Json(data);
        }

        public ActionResult ViewEquityPriceHistory()
        {
            var oldModel = (EditEquityHeaderModel)TempData["EditEquityHeaderModel"];
            return View("ViewEquityPriceHistory", oldModel);
        }
        #endregion



        #region Transaction -->Purchase

        public ActionResult AddPurchaseTransaction(int headerId)
        {
            EditEquityPurchaseModel model = null;
            try
            {
                var data = Service.GetEquityTransactionScreenData(headerId, null, "Purchase");
                model = EditEquityPurchaseModel.BuildModel(data);
                TempData["EditEquityPurchaseModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityPurchaseModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquityPurchaseTransaction", model);
        }

        public ActionResult EditPurchaseTransaction(int transactionId)
        {
            EditEquityPurchaseModel model = null;
            try
            {
                var data = Service.GetEquityTransactionScreenData(null, transactionId, "Purchase");
                model = EditEquityPurchaseModel.BuildModel(data);
                model.UpdateMode = true;
                TempData["EditEquityPurchaseModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityPurchaseModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquityPurchaseTransaction", model);
        }

        [HttpPost]
        public ActionResult EditPurchaseTransaction(EditEquityPurchaseModel model)
        {
            var oldModel = (EditEquityPurchaseModel)TempData["EditEquityPurchaseModel"];
            if (ModelState.IsValid)
            {
                try
                {
                    if (!String.IsNullOrEmpty(model.SubmitBtn))
                    {
                        var broker = oldModel.BrokerList.SingleOrDefault(x => x.Value == model.Transaction.BrokerID.GetValueOrDefault().ToString());
                        if (broker != null)
                            model.Transaction.Broker = broker.Text;

                        if (model.UpdateMode)
                        {
                            model.Transaction.ModifyBY = ServiceFactory.UserId;
                            model.Transaction.CreateBY = oldModel.Transaction.CreateBY;
                            model.Transaction.CreateDate = oldModel.Transaction.CreateDate;
                            model.Transaction.ModifyDate = oldModel.Transaction.ModifyDate;
                            oldModel.Transaction = Service.UpdateEquityTransaction(model.Transaction);
                        }
                        else
                        {
                            model.Transaction.CreateBY = ServiceFactory.UserId;
                            oldModel.Transaction = Service.InsertEquityTransaction(model.Transaction);
                        }
                        oldModel.UpdateMode = true;
                        ViewBagUtil.SetCompletedInfo(ViewBag);
                    }
                    else
                    {
                        throw new Exception("Button save (BtnSave) is null.");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    //ModelState.AddModelError("", ex.Message);
                    ViewBagUtil.SetError(ViewBag, ex.Message, null);
                }
            }
            TempData["EditEquityPurchaseModel"] = oldModel;
            return View("EditEquityPurchaseTransaction", oldModel);
        }
        #endregion

        #region Transaction -->Sale

        public ActionResult AddSaleTransaction(int headerId)
        {
            EditEquitySaleModel model = null;
            try
            {
                var data = Service.GetEquityTransactionScreenData(headerId, null, "Sale");
                model = EditEquitySaleModel.BuildModel(data);
                TempData["EditEquitySaleModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquitySaleModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquitySaleTransaction", model);
        }

        public ActionResult EditSaleTransaction(int transactionId)
        {
            EditEquitySaleModel model = null;
            try
            {
                var data = Service.GetEquityTransactionScreenData(null, transactionId, "Sale");
                model = EditEquitySaleModel.BuildModel(data);
                model.UpdateMode = true;
                TempData["EditEquitySaleModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquitySaleModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquitySaleTransaction", model);
        }

        [HttpPost]
        public ActionResult EditSaleTransaction(EditEquitySaleModel model)
        {
            var oldModel = (EditEquitySaleModel)TempData["EditEquitySaleModel"];
            if (ModelState.IsValid)
            {
                try
                {
                    if (!String.IsNullOrEmpty(model.SubmitBtn))
                    {
                        if (model.UpdateMode)
                        {
                            model.Transaction.ModifyBY = ServiceFactory.UserId;
                            model.Transaction.CreateBY = oldModel.Transaction.CreateBY;
                            model.Transaction.CreateDate = oldModel.Transaction.CreateDate;
                            model.Transaction.ModifyDate = oldModel.Transaction.ModifyDate;
                            oldModel.Transaction = Service.UpdateEquityTransaction(model.Transaction);
                        }
                        else
                        {
                            model.Transaction.CreateBY = ServiceFactory.UserId;
                            oldModel.Transaction = Service.InsertEquityTransaction(model.Transaction);
                        }
                        oldModel.UpdateMode = true;
                        ViewBagUtil.SetCompletedInfo(ViewBag);
                    }
                    else
                    {
                        throw new Exception("Button save (BtnSave) is null.");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    //ModelState.AddModelError("", ex.Message);
                    ViewBagUtil.SetError(ViewBag, ex.Message, null);
                }
            }
            TempData["EditEquitySaleModel"] = oldModel;
            return View("EditEquitySaleTransaction", oldModel);
        }
        #endregion

        #region Transaction -->Guarantee

        public ActionResult AddGuaranteeTransaction(int headerId)
        {
            EditEquityGuaranteeModel model = null;
            try
            {
                var data = Service.GetEquityTransactionScreenData(headerId, null, "Guarantee");
                model = EditEquityGuaranteeModel.BuildModel(data);
                TempData["EditEquityGuaranteeModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityGuaranteeModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquityGuaranteeTransaction", model);
        }

        [HttpGet]
        public ActionResult EditGuaranteeTransaction(int transactionId)
        {
            EditEquityGuaranteeModel model = null;
            try
            {
                var data = Service.GetEquityTransactionScreenData(null, transactionId, "Guarantee");
                model = EditEquityGuaranteeModel.BuildModel(data);
                model.UpdateMode = true;
                TempData["EditEquityGuaranteeModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityGuaranteeModel();
                //ModelState.AddModelError("", ex.Message);
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquityGuaranteeTransaction", model);
        }

        [HttpPost]
        public ActionResult EditGuaranteeTransaction(EditEquityGuaranteeModel model)
        {
            var oldModel = (EditEquityGuaranteeModel)TempData["EditEquityGuaranteeModel"];
            if (ModelState.IsValid)
            {
                try
                {
                    if (!String.IsNullOrEmpty(model.SubmitBtn))
                    {
                        if (model.UpdateMode)
                        {
                            model.Transaction.ModifyBY = ServiceFactory.UserId;
                            model.Transaction.CreateBY = oldModel.Transaction.CreateBY;
                            model.Transaction.CreateDate = oldModel.Transaction.CreateDate;
                            model.Transaction.ModifyDate = oldModel.Transaction.ModifyDate;
                            oldModel.Transaction = Service.UpdateEquityTransaction(model.Transaction);
                        }
                        else
                        {
                            model.Transaction.CreateBY = ServiceFactory.UserId;
                            oldModel.Transaction = Service.InsertEquityTransaction(model.Transaction);
                        }
                        oldModel.UpdateMode = true;
                        ViewBagUtil.SetCompletedInfo(ViewBag);
                    }
                    else
                    {
                        throw new Exception("Button save (BtnSave) is null.");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    //ModelState.AddModelError("", ex.Message);
                    ViewBagUtil.SetError(ViewBag, ex.Message, null);
                }
            }
            TempData["EditEquityGuaranteeModel"] = oldModel;
            return View("EditEquityGuaranteeTransaction", oldModel);
        }
        #endregion

        #region Transaction --> Privilege

        public ActionResult ManageEquityPrivilege(int headerId)
        {
            ManageEquityPrivilegeModel model = null;
            try
            {
                var data = Service.GetEquityPrivilegeDataByHeaderId(headerId);
                model = ManageEquityPrivilegeModel.BuildModel(data);
            }
            catch (Exception ex)
            {
                model = new ManageEquityPrivilegeModel();
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            TempData["ManageEquityPrivilegeModel"] = model;
            return View("ManageEquityPrivilege", model);
        }

        public ActionResult AddEquityPrivilege(int headerId)
        {
            EditEquityPrivilegeModel model = null;
            try
            {
                var data = Service.GetEquityPrivilegeScreenData(headerId, null);
                model = EditEquityPrivilegeModel.BuildModel(data);
                TempData["EditEquityPrivilegeModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityPrivilegeModel();
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquityPrivilege", model);
        }

        public ActionResult EditEquityPrivilege(int privilegeId)
        {
            EditEquityPrivilegeModel model = null;
            try
            {
                var data = Service.GetEquityPrivilegeScreenData(null, privilegeId);
                model = EditEquityPrivilegeModel.BuildModel(data);
                model.UpdateMode = true;
                TempData["EditEquityPrivilegeModel"] = model;
            }
            catch (Exception ex)
            {
                model = new EditEquityPrivilegeModel();
                ViewBagUtil.SetError(ViewBag, ex.Message, null);
            }
            return View("EditEquityPrivilege", model);
        }

        [HttpPost]
        public ActionResult EditEquityPrivilege(EditEquityPrivilegeModel model)
        {
            var oldModel = (EditEquityPrivilegeModel)TempData["EditEquityPrivilegeModel"];
            if (ModelState.IsValid)
            {
                try
                {
                    if (!String.IsNullOrEmpty(model.SubmitBtn))
                    {
                        var item = oldModel.TypeList.SingleOrDefault(x => x.Value == model.Privilege.PrivilegeTypeID.GetValueOrDefault().ToString());
                        if (item != null)
                            model.Privilege.PrivilegeTypeName = item.Text;

                        if (model.UpdateMode)
                        {
                            model.Privilege.ModifyBY = ServiceFactory.UserId;
                            model.Privilege.CreateBY = oldModel.Privilege.CreateBY;
                            model.Privilege.CreateDate = oldModel.Privilege.CreateDate;
                            model.Privilege.ModifyDate = oldModel.Privilege.ModifyDate;
                            oldModel.Privilege = Service.UpdateEquityPrivilege(model.Privilege);
                        }
                        else
                        {
                            model.Privilege.CreateBY = ServiceFactory.UserId;
                            oldModel.Privilege = Service.InsertEquityPrivilege(model.Privilege);
                        }
                        oldModel.UpdateMode = true;
                        ViewBagUtil.SetCompletedInfo(ViewBag);
                    }
                    else
                    {
                        throw new Exception("Button save (BtnSave) is null.");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                    ViewBagUtil.SetError(ViewBag, ex.Message, null);
                }
            }
            TempData["EditEquityPrivilegeModel"] = oldModel;
            return View("EditEquityPrivilege", oldModel);
        }


        #endregion
    }
}
